/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.pay.api;

import cn.hutool.core.bean.BeanUtil;
import com.ijpay.core.enums.TradeType;
import com.lxk.pay.PayService;
import com.lxk.pay.model.*;
import com.lxk.service.pay.api.PayServiceApi;
import com.lxk.service.pay.enums.TradeTypeEnum;
import com.lxk.service.pay.param.OrderQueryParam;
import com.lxk.service.pay.param.PayRefundParam;
import com.lxk.service.pay.param.PayRequestParam;
import com.lxk.service.pay.response.OrderQueryResultResponse;
import com.lxk.service.pay.response.PayResultResponse;
import com.lxk.service.pay.response.RefundResultResponse;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.ValidationUtils;
import org.springframework.stereotype.Service;

import static com.lxk.service.pay.constants.PayServiceBeanConstants.*;

/**
 * 支付 Service 实现类
 *
 * @author huahua
 */
@Service
@AllArgsConstructor
public class PayServiceApiImpl implements PayServiceApi {

    @Override
    public PayResultResponse pay(PayRequestParam param) {
        ValidationUtils.validate(param);
        PayRequestModel payRequestModel = BeanUtil.copyProperties(param, PayRequestModel.class);
        String beanName = getBeanName(param.getPayType(), payRequestModel);
        PayService payService = SpringUtils.getBean(beanName, PayService.class);
        PayResultModel payResultModel = payService.pay(payRequestModel);
        return BeanUtil.copyProperties(payResultModel, PayResultResponse.class);
    }

    @Override
    public RefundResultResponse refund(PayRefundParam param) {
        ValidationUtils.validate(param);
        PayRefundModel payRequestModel = MapstructUtils.convert(param, PayRefundModel.class);
        String beanName = getBeanName(param.getTradeType(), payRequestModel);
        PayService payService = SpringUtils.getBean(beanName, PayService.class);
        RefundResultModel resultModel = payService.refund(payRequestModel);
        return BeanUtil.copyProperties(resultModel, RefundResultResponse.class);
    }

    @Override
    public OrderQueryResultResponse orderQuery(OrderQueryParam param) {
        ValidationUtils.validate(param);
        OrderQueryRequestModel requestModel = BeanUtil.copyProperties(param, OrderQueryRequestModel.class);
        String beanName = getBeanName(param.getPayType(), new PayRequestModel());
        PayService payService = SpringUtils.getBean(beanName, PayService.class);
        OrderQueryResultModel resultModel = payService.orderQuery(requestModel);
        return BeanUtil.copyProperties(resultModel, OrderQueryResultResponse.class);
    }

    private String getBeanName(String payType, BasePayModel model) {
        String beanName;
        if (TradeTypeEnum.WX_APP.getTradeType().equals(payType)) {
            beanName = WX_PAY_SERVICE;
            model.setTradeTypeForThird(TradeType.APP.getTradeType());
        } else if (TradeTypeEnum.WX_JSAPI.getTradeType().equals(payType)) {
            beanName = WX_PAY_SERVICE;
            model.setTradeTypeForThird(TradeType.JSAPI.getTradeType());
        } else if (TradeTypeEnum.WX_MWEB.getTradeType().equals(payType)) {
            beanName = WX_PAY_SERVICE;
            model.setTradeTypeForThird(TradeType.MWEB.getTradeType());
        } else if (TradeTypeEnum.ALI_APP.getTradeType().equals(payType)) {
            beanName = ALI_PAY_SERVICE;
            model.setTradeTypeForThird(TradeType.APP.getTradeType());
        } else if (TradeTypeEnum.ALI_MWEB.getTradeType().equals(payType)) {
            beanName = ALI_PAY_SERVICE;
            model.setTradeTypeForThird(TradeType.MWEB.getTradeType());
        } else {
            beanName = WALLET_PAY_SERVICE;
        }
        return beanName;
    }
}
